<?php
class Cover extends Vfriend_Db {	
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function get($intCoverId) {
		try {		
			$strKey 	= COVER_GET . $intCoverId;
			$arrResult	= $this->cache->get($intCoverId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call covers_get(?)');
				$this->bindParam(1, $intCoverId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, COVER_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Cover::get - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getCoverByUserId($intUserId) {
		try {		
			$strKey 	= COVER_GETCOVERBYUSERID . $intUserId;
			$arrResult	= $this->cache->get($intUserId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ucovers_get(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, COVER_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Cover::getCoverByUserId - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getAll($intUserId=-1) {
		try {	
			$strKey 	= COVER_GETALL . $intUserId ;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call covers_getall(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();			
				$this->cache->set($strKey, $arrResult, COVER_GETALL_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Cover::getAll - ' .$e->getMessage());			
			return null;
		}
	}	
	public function add($intUserId, $intHexId, $strImage, $strName='', $intDisorder=0, $intAdminId=0) {
		try {			
			$this->prepare('call covers_add(?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, PDO::PARAM_STR, 150);
			$this->bindParam(4, $strImage, PDO::PARAM_STR, 150);
			$this->bindParam(5, $intDisorder, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				if(FUNCTION_PAYMENT_COVER && PAYMENT_COVER) {
					$this->clear->deleteAccount($intUserId);
				}
				$this->clear->deleteCoverByUserId($intUserId);
				$strImage	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				$this->deleteImg($strImage,$intHexId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Cover::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function insertUcover($intUserId, $intCoverId, $intTop=0, $strPath='') {
		try {				
			$this->prepare('call ucovers_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intCoverId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $strPath, PDO::PARAM_STR, 150);	
			$this->bindParam(4, $intTop, PDO::PARAM_INT, 6);	
			if($this->execute()) {
				$this->clear->deleteCoverByUserId($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Cover::updatePathExtend - ' .$e->getMessage());			
			return null;
		}
	}
	public function updatePathExtend($intUserId, $intCoverId, $intTop=0, $strPath='') {
		try {				
			$this->prepare('call ucovers_update_path(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intCoverId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $strPath, PDO::PARAM_STR, 150);	
			$this->bindParam(4, $intTop, PDO::PARAM_INT, 6);	
			if($this->execute()) {
				$this->clear->deleteCoverByUserId($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Cover::updatePathExtend - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImg($strImg, $intHexId) {
		if($strImg != '') {
			$strPlace	= '_' . $intHexId . '.';
			$strExImg	= str_replace('.', $strPlace, $strImg);
			if(file_exists(DIR_COVER_FULL . $strImg))
				@unlink(DIR_COVER_FULL . $strImg);
			if(file_exists(DIR_COVER_THUMB . $strImg))
				@unlink(DIR_COVER_THUMB . $strImg);
			if(file_exists(DIR_COVER_THUMB . $strExImg))
				@unlink(DIR_COVER_THUMB . $strExImg);
			if(file_exists(DIR_COVER_SMALL . $strImg))
				@unlink(DIR_COVER_SMALL . $strImg);
		}
	}
}
?>